//by:PJ小宇
//QQ:898811295
run();//计时
device.wakeUp();//亮屏
//检查无障碍权限申请
auto.waitFor();
//设置屏幕宽度高度
//setScreenMetrics(1080, 1920);
//悬浮窗
var w = floaty.rawWindow(
    <frame gravity="center" bg="#CC999999">
        <text padding="5 0 5 0" w="auto" h="auto" id="text" textColor='#FFFFFF'></text>
    </frame>
);
w.setPosition(device.width*0.06,device.height*0.1);
w.setTouchable(false);
ui.run(function () {
        w.text.setText("默认为作者助力，介意勿用！如有疑问联系QQ：898811295");
    });
var AppName = "快手极速版";
var AppName_JB = "快手极速版脚本";
var time1 =random(5000,11000); 
var 金币转圈 = id("redFloat");
var 金币转圈x = 127.5;
var 金币转圈y = 1218.5;
var 点赞 = id("like_button");
var 现金收益 = text("现金收益");
var 去签到 = text("去签到");
var 立即签到 = text("立即签到");
var 今天已签 = textContains("今天已签");
var 青少年模式 =  text("青少年模式");
var 我知道了 =  text("我知道了");
var 拖动滑块 = text("拖动滑块");
var 去填写 = text("去填写");
var 拖动滑块x = 123;
var 拖动滑块y = 976.5;
var 立即邀请 = text("立即邀请");
var 首页 = text("首页");
var 看视频最高得 = text("看视频最高得");
var time2 =random(600,1000);
var time3 =random(5300,7000);
if(!requestScreenCapture()){
    ui.run(function () {
        w.text.setText("请求截图失败");
    });
    stop();
}
//app.launchApp(AppName);
//签到
// SignIn();
// sleep(random(1000,2000))
killAPP("快手极速版")
sleep(time2);
ks2();
sleep(200);
setClip(lk);
ui.run(function () {
    w.text.setText("正在打开快手极速版（本次为助力篇）");
});
launchApp("快手极速版");
sleep(9500);
ks3();
sleep(500);
setClip(week);
sleep(2000);
home();
sleep(4000);
launchApp("快手极速版");
sleep(1000);
while(true){
        //检测青少年模式
        if(青少年模式.findOne(1200) != null){
            ui.run(function () {
                w.text.setText("正在跳过青少年模式");
            });
            sleep(1000);
            我知道了.findOne().click();
        }
        //立即邀请
        if(立即邀请.findOne(1000) != null){
            console.log("立即邀请");
            sleep(1000);
            back();
        }
        //检测滑块
        if(拖动滑块.findOne(1000) != null){
            ui.run(function () {
                w.text.setText("检测到滑块");
            });
            dragSlider();
        }
sleep(6000);
!click("为ta助力");
sleep(time3);
//检测首页
function first(){
while(true){
if(textContains("首页").exists()){
    sleep(time2)
    !click("首页");
    sleep(time2)
    break;
  }
back();
sleep(1000);
 }
}
first();
//由于种种原因提前提取"去赚钱“坐标
    var activon = textContains("去赚钱").boundsInside(0, 0, device.width, device.height).findOne()
    var activon_bound = activon.bounds()
    var x6 = activon_bound.left
    var y6 = activon_bound.top
    var x7 = activon_bound.right
    var y7 = activon_bound.bottom
    var x =random(x6,x7);
    var y =random(y6,y7);
    sleep(time2);
    click(x,y);
//*/
sleep(time2)
while(true){
    if(textContains("看视频最高得").exists()){
        sleep(time2)
        back();
        sleep(time2)
        var x =random(x6,x7);
        var y =random(y6,y7);
        click(x,y);
        break;
      }
     break;
     }
sleep(time2);
code();
sleep(2000);
first();
sleep(time2);
var x =random(x6,x7);
var y =random(y6,y7);
click(x,y);
sleep(time2);
while(true){
    if(textContains("看视频最高得").exists()){
        sleep(time2)
        back();
        sleep(time2)
        var x =random(x6,x7);
        var y =random(y6,y7);
        click(x,y);
        break;
      }
     break;
     }
sleep(time2);
ui.run(function () {
    w.text.setText("正在检测日常任务");
});
//!click("去赚钱");
sleep(time2);
today();
ui.run(function () {
    w.text.setText("未检出可做任务或日常任务已全部完成，即将进行浏览视频~");
});
sleep(8000)
break;
}
//上面为循环
first();
sleep(time2);
!click("首页");
sleep(time2);
slidingVideo();
sleep(4000)
//新人专属
var x =random(x6,x7);
var y =random(y6,y7);
click(x,y);
sleep(time2);
for(var num=1;num<=5;num++){
    //领取奖励
    if(textContains("立即领取").exists()){
        ui.run(function () {
    w.text.setText("准备领取新人福利");
    });
    !click("立即领取");
    sleep(3000);
    }
}
/*
sleep(3000);
code();
sleep(1000);
launchApp("快手极速版");
ui.run(function () {
        w.text.setText("正在打开快手极速版……");
    });
*/
    sleep(7000);
//slidingVideo();
killAPP(AppName);
/**
 * 签到
 */
function SignIn(){
    while(true){
        sleep(random(1000,2000));
        if(金币转圈.findOne(1000) != null){
            console.log("金币转圈---已加载");
            sleep(random(1000,2000));
            click(金币转圈x,金币转圈y);
            console.log("金币转圈---已点击");
            while(true){
                sleep(random(1000,2000));
                if(现金收益.findOne(1000) != null){
                    console.log("现金收益---已加载");
                    while(true){
                        sleep(random(1000,2000));
                        if(立即签到.findOne(1000) != null){
                            sleep(random(1000,2000));
                            立即签到.findOne().click();
                        }else if(今天已签.exists()){
                            console.log("今天已签---已加载");
                            sleep(random(1000,2000));
                            back();
                            console.log("金币转圈---已加载");
                            break;   
                        }
                    }
                    break;
                }
            }
            break;
        }
    }
}
function swipe(){
    var x1 = random(device.width*0.256,device.width*0.706);
    var y1 = random(device.height/9*6,device.height/8*6);
    var x2 = random(device.width*0.256,device.width*0.706);
    var y2 = random(device.height/15,device.height/12);
   // var time2 =random(600,1000);       
sml_move(x1, y1, x2, y2,time2);
} 
function swipe2(){
    var x1 = random(device.width*0.256,device.width*0.706);
    var y2 = random(device.height/15*12,device.height/15*14);
    var x2 = random(device.width*0.256,device.width*0.706);
    var y1 = random(device.height/7,device.height/5);
   // var time2 =random(600,1000);       
sml_move(x1, y1, x2, y2,time2);
} 
/**
 * 滑动视频
 */
function slidingVideo(){
    for(var num=1;num<=1000;num++){
        //检测青少年模式
        if(青少年模式.findOne(1000) != null){
            ui.run(function () {
                w.text.setText("正在跳过青少年模式");
            });
            sleep(1000);
            我知道了.findOne().click();
        }
        //立即邀请
        if(立即邀请.findOne(1000) != null){
            console.log("立即邀请");
            sleep(1000);
            back();
        }
        //检测滑块
        if(拖动滑块.findOne(1000) != null){
            ui.run(function () {
                w.text.setText("检测到滑块");
            });
            dragSlider();
        }
        if(textContains("查看长图").exists()){
        ui.run(function () {
        w.text.setText("貌似是长图，跳过");
       });
        swipe();
        sleep(1000);
        }
        if(textContains("查看图集").exists()){
            ui.run(function () {
                w.text.setText("貌似是图集，跳过");
            });
                swipe();
                 sleep(1000);
        }
        if(textContains("进入直播间").exists()){
            ui.run(function () {
                w.text.setText("貌似是直播，跳过");
            });
                swipe();
                sleep(1000);
        }
        //开始滑动视频 
        if(textContains("首页").exists()){
            ui.run(function () {
                w.text.setText("正在刷视频中~");    
            });   
        swipe(); 
        if(textContains("中国").exists()){
            ui.run(function () {
                w.text.setText("正在点赞中~");    
            }); 
            let h = random(0.4,0.55);  
            sleep(time2);
            click(device.width*h,device.height*h);
            sleep(100);
            click(device.width*h,device.height*h);
            sleep(time2);
        }   
        if(textContains("35").exists()){
            ui.run(function () {
                w.text.setText("正在回看视频中~");    
            }); 
            swipe2();    
            sleep(time1);
            ui.run(function () {
                w.text.setText("正在刷视频中~");    
            });
            swipe(); 
        }
           sleep(time1); 
        }
        launchApp("快手极速版");
    }
} 

/**
 * 拖动滑块
 * 
 */
function dragSlider() {
    for (var i = 0; i < 0; i++) { sleep(1000); log(i); }
    while (true) {
        img = images.captureScreen();
        if (img) {
            log("截图成功。进行识别滑块！");
            break;
        } else {
            log('截图失败,重新截图');
        }
    }
    var x = discernSlidingblock(img, device.width) + 65
    console.info("识别结果滑块X坐标：" + x);
 
    if (x > -1) {
        randomSwipe(拖动滑块x, 拖动滑块y, x, 拖动滑块y)
        return true;
    } else {
        return false;
        console.log("识别有误，请确认是否在滑块界面");
    }
}

function code(){
for(var num=1;num<=5;num++){
    if(去填写.findOne(3000) != null){
        sleep(1000);
        ui.run(function () {
            w.text.setText("准备领取新人福利");
            });
    去填写.findOne().click();
     sleep(1000);
     ks1();
     sleep(1000);
     click(device.width/2,device.height*0.439);
     sleep(time3);
     setText(0,key);
     sleep(1000);
     !click("提交领现金")
     sleep(1500);
     back()
    } 
    ui.run(function () {
        w.text.setText("快手欢迎你，老油条~");
        });
  }
}
function ks1(){
let url = "https://gitee.com/pj-xiaoyu/auto/raw/master/ks/code";
let r = http.get(url);
key = r.body.string(); 
};
function killAPP(name){
   var packageName=app.getPackageName(name)
app.openAppSetting(packageName)
sleep(4000)
while(true){
   if(textContains("强行停止").exists()){
        !click("强行停止");
        sleep(1000);
        while(true){
             if(textContains("确定").exists()){
                // !click("确定");
                 sleep(time2*2);
                 !click("强行停止");
                 break;
             }
             break;
        }
        break;
     }
    stop();
    break;
  }
 back();
}
function stop(){
while(true){
   if(textContains("结束运行").exists()){
        !click("结束运行");
        sleep(1000);
        while(true){
             if(textContains("确定").exists()){
                 !click("确定");
                 sleep(500);
                 break;
             }
             break;
        }
        break;
     }
  }
 back();
}

function today(){
    sleep(time2);
        swipe();
    for(var num=1;num<=8;num++){
        if(textContains("去签到").exists()){
            ui.run(function () {
                w.text.setText("签到中~");
            });
                !click("去签到");
                 sleep(2500);
        }
        if(textContains("开宝箱得金币").exists()){
            ui.run(function () {
                w.text.setText("开宝箱中~");
            });
                !click("开宝箱得金币");
                sleep(time2);
                back();
        }
        //点击翻倍
        if(textContains("点击翻倍").exists()){
            ui.run(function () {
        w.text.setText("已找到点击翻倍，准备翻倍！");
        });
        !click("点击翻倍");
        sleep(1000);
        }
        if(textContains("去补签").exists()){
            ui.run(function () {
                w.text.setText("昨日未签到，补签中~");
            });
                !click("去补签");
                sleep(time2*2);
                !click("补签再得");

        }
        sleep(time2)
        while(true){
            if(textContains("首页").exists()){
                sleep(time2);
                var x =random(x6,x7);
                var y =random(y6,y7);
                click(x,y);
                break;
              }
             break;
             }
        sleep(time2);
        swipe();
    }
    ui.run(function () {
        w.text.setText("未检出可做任务，即将进行正常浏览视频~");
    });
}

function ks2(){
let url = "https://gitee.com/pj-xiaoyu/auto/raw/master/ks/lk";
let r = http.get(url);
lk = r.body.string(); 
};

function run() { //声明运行函数
    threads.start(function() {
        startTime = new Date().getTime();
    
        while (true) {
            runTime();
            sleep(1000);
        }
    });
}
function ks3(){
let url = "https://gitee.com/pj-xiaoyu/auto/raw/master/ks/week";
let r = http.get(url);
week = r.body.string(); 
};
function runTime() {
    var endTime = new Date().getTime();
    var spendTime = Math.floor((endTime - startTime) / 1000);
  //  log('已等待%m秒', spendTime);
    let t = 11000;
    if (spendTime >= t) {
        console.info("定时已结束");
        launchApp("AutoJsPro");
        exit();
    }
}

/**
 * 计算滑块位置
 * @param {图片} img 
 * @param {分辨率} ratio 
 */
function discernSlidingblock(img, ratio) {
    //创建识别变量
    var temp, temp2, x, y, num, color, p, temp3, arr1;
    //分析设备分辨率
    if (ratio == 720) {
        var tb = [348, 253, 691, 638, 81]
        log("您的设备分辨率为：720p");
    } else if (ratio == 1080) {
        var tb = [463, 387, 912, 831, 125]
        log("您的设备分辨率为：1080p");
    } else {
        log("当前设备分辨率不符合规范")
        return -2
    }
    num = Math.ceil(tb[4] / 3.3 - 4);
 
    //计算滑块位置
    for (var k = 29; k <= 40; k++) {
        temp2 = "";
        color = "#" + k + "" + k + "" + k + "";
        for (var i = 1; i <= num; i++) {
            temp2 = temp2 + "0|" + i + "|" + color + ",";
            temp2 = temp2 + i + "|0|" + color + ",";
            temp2 = temp2 + "1|" + i + "|" + color + ",";
            temp2 = temp2 + i + "|1|" + color + ",";
            temp2 = temp2 + "2|" + i + "|" + color + ",";
            temp2 = temp2 + i + "|2|" + color + ",";
        }
        x = 0;
        while (x > -2) {
            y = 0;
            while (y > -2) {
                temp = "";
                for (var i = 1; i <= num; i += 2) {
                    temp = temp + "0|" + (tb[4] + y - i - 1) + "|" + color + ",";
                    temp = temp + (tb[4] + y) + "|" + i + "|" + color + ",";
                    temp = temp + (tb[4] + x) + "|" + (tb[4] + y - i - 1) + "|" + color + ",";
                    temp = temp + (tb[4] + x - i - 1) + "|0|" + color + ",";
                    temp = temp + i + "|" + (tb[4] + y) + "|" + color + ",";
                    temp = temp + (tb[4] + x - i - 1) + "|" + (tb[4] + y) + "|" + color + ",";
                    temp = temp + "1|" + (tb[4] + y - i - 1) + "|" + color + ",";
                    temp = temp + (tb[4] + x - 1) + "|" + i + "|" + color + ",";
                    temp = temp + (tb[4] + x - 1) + "|" + (tb[4] + y - i - 1) + "|" + color + ",";
                    temp = temp + (tb[4] + x - i - 1) + "|1|" + color + ",";
                    temp = temp + i + "|" + (tb[4] + y - 1) + "|" + color + ",";
                    temp = temp + (tb[4] + y- i - 1) + "|" + (tb[4] + y - 1) + "|" + color + ",";
                }
                temp = temp + temp2 + "0|0|" + color;
                arr1 = temp.split(",");
                var arr2 = new Array();
                for (var i = 0; i < arr1.length - 1; i++) {
                    arr2[i] = new Array();
                    temp3 = arr1[i].split("|");
                    arr2[i] = [Number(temp3[0]), Number(temp3[1]), temp3[2]];
                }
                try {
                    p = images.findMultiColors(img, color, arr2, {
                        region: [tb[0], tb[1], tb[2] - tb[0], tb[3] - tb[1]],
                        threshold: (Math.floor(k / 10) * 16 + k % 10)
                    });
                    if (p) {
                        img.recycle();
                        return p.x
                    }
                } catch (error) {
                    //出错
                    console.log("识别失败，错误原因：" + error);
                    return -1;
                }
                y = --y;
            }
            x = --x;
        }
    }
    try {
        img.recycle();
    } catch (error) {
        console.log("识别失败，错误原因：" + error);
    }
    return -1;
}
/**
 * 真人模拟滑动函数 （滑块滑动）
 * @param {起点x} sx 
 * @param {起点y} sy 
 * @param {终点x} ex 
 * @param {终点y} ey 
 */
function randomSwipe(sx, sy, ex, ey) {
    //设置随机滑动时长范围
    var timeMin = 1000
    var timeMax = 3000
    //设置控制点极限距离
    var leaveHeightLength = 500
 
    //根据偏差距离，应用不同的随机方式
    if (Math.abs(ex - sx) > Math.abs(ey - sy)) {
        var my = (sy + ey) / 2
        var y2 = my + random(0, leaveHeightLength)
        var y3 = my - random(0, leaveHeightLength)
 
        var lx = (sx - ex) / 3
        if (lx < 0) { lx = -lx }
        var x2 = sx + lx / 2 + random(0, lx)
        var x3 = sx + lx + lx / 2 + random(0, lx)
    } else {
        var mx = (sx + ex) / 2
        var y2 = mx + random(0, leaveHeightLength)
        var y3 = mx - random(0, leaveHeightLength)
 
        var ly = (sy - ey) / 3
        if (ly < 0) { ly = -ly }
        var y2 = sy + ly / 2 + random(0, ly)
        var y3 = sy + ly + ly / 2 + random(0, y3)
    }   
 
    //获取运行轨迹，及参数
    var time = [0, random(timeMin, timeMax)]
    var track = bezierCreate(sx, sy, x2, y2, x3, y3, ex, ey)
 
/*    
    log("随机控制点A坐标：" + x2 + "," + y2)
    log("随机控制点B坐标：" + x3 + "," + y3)
    log("随机滑动时长：" + time[1])
 */
    //滑动
    gestures(time.concat(track))
}
/**
 * 计算滑动轨迹
 */
function bezierCreate(x1, y1, x2, y2, x3, y3, x4, y4) {
    //构建参数
    var h = 100;
    var cp = [{ x: x1, y: y1 + h }, { x: x2, y: y2 + h }, { x: x3, y: y3 + h }, { x: x4, y: y4 + h }];
    var numberOfPoints = 100;
    var curve = [];
    var dt = 1.0 / (numberOfPoints - 1);
 
    //计算轨迹
    for (var i = 0; i < numberOfPoints; i++) {
        var ax, bx, cx;
        var ay, by, cy;
        var tSquared, tCubed;
        var result_x, result_y;
 
        cx = 3.0 * (cp[1].x - cp[0].x);
        bx = 3.0 * (cp[2].x - cp[1].x) - cx;
        ax = cp[3].x - cp[0].x - cx - bx;
        cy = 3.0 * (cp[1].y - cp[0].y);
        by = 3.0 * (cp[2].y - cp[1].y) - cy;
        ay = cp[3].y - cp[0].y - cy - by;
 
        var t = dt * i
        tSquared = t * t;
        tCubed = tSquared * t;
        result_x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
        result_y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
        curve[i] = {
            x: result_x,
            y: result_y
        };
    }
 
    //轨迹转路数组
    var array = [];
    for (var i = 0; i < curve.length; i++) {
        try {
            var j = (i < 100) ? i : (199 - i);
            xx = parseInt(curve[j].x)
            yy = parseInt(Math.abs(100 - curve[j].y))
        } catch (e) {
            break
        }
        array.push([xx, yy])
    }
 
    return array
}
/**
 * 仿真随机带曲线滑动  (视频滑动)
 * @param {起点x} qx 
 * @param {起点y} qy 
 * @param {终点x} zx 
 * @param {终点y} zy 
 * @param {过程耗时单位毫秒} time 
 */
function sml_move(qx, qy, zx, zy, time) {
    var xxy = [time];
    var point = [];
    var dx0 = {
        "x": qx,
        "y": qy
    };
 
    var dx1 = {
        "x": random(qx - 100, qx + 100),
        "y": random(qy , qy + 50)
    };
    var dx2 = {
        "x": random(zx - 100, zx + 100),
        "y": random(zy , zy + 50),
    };
    var dx3 = {
        "x": zx,
        "y": zy
    };
    for (var i = 0; i < 4; i++) {
 
        eval("point.push(dx" + i + ")");
 
    };
 //   log(point[3].x)
 
    for (let i = 0; i < 1; i += 0.08) {
        xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]
 
        xxy.push(xxyy);
 
    }
 
    log(xxy);
    gesture.apply(null, xxy);
};
function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x); 
    bx = 3.0 * (cp[2].x - cp[1].x) - cx; 
    ax = cp[3].x - cp[0].x - cx - bx; 
    cy = 3.0 * (cp[1].y - cp[0].y); 
    by = 3.0 * (cp[2].y - cp[1].y) - cy; 
    ay = cp[3].y - cp[0].y - cy - by; 
    
    tSquared = t * t; 
    tCubed = tSquared * t; 
    result = {
        "x": 0,
        "y": 0
    };
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x; 
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y; 
    return result; 
};
